//
//  PasswordVC.m
//  Diary
//
//  Created by lanou3g on 15/11/4.
//  Copyright © 2015年 王艺拓. All rights reserved.
//

#import "PasswordVC.h"
#import "WYTDeskVC.h"
#import "WYTDevicesTool.h"

#define kSelfWidth [UIScreen mainScreen].bounds.size.width
#define kSelfHeight [UIScreen mainScreen].bounds.size.height
#define kTopViewSizeH kSelfHeight*0.065
#define kTopImageSizeW kNumberItemSide*3*0.75*0.25
#define kTopViewOrigineX (kSelfWidth - kNumberItemSide*3*0.75)/2.0
#define kTopViewOrigineY kSelfHeight*0.13  // topView起始纵坐标
#define kTopViewOrigineY_Iphone4 kSelfHeight*0.08
#define kNumberItemSide (kSelfWidth*0.77)/3.0
#define kNumberItemOrigineY kSelfHeight*0.25  // item起始纵坐标
#define kNumberItemOrigineY_Iphone4 kSelfHeight*0.16
#define kNumberItemOrigineX (kSelfWidth - kNumberItemSide*3.0)/2.0

#define kZeroBtnTag  286010
#define kOneBtnTag   286011
#define kTwoBtnTag   286012
#define kThreeBtnTag 286013
#define kFourBtnTag  286014
#define kFiveBtnTag  286015
#define kSixBtnTag   286016
#define kSevenBtnTag 286017
#define kEightBtnTag 286018
#define kNineBtnTag  286019
#define kCancelBtnTag 286020


@interface PasswordVC ()

@property (nonatomic, strong) UIView *topView;
// A ~ D
@property (nonatomic, strong) UIImageView *firstImg;
@property (nonatomic, strong) UIImageView *secondImg;
@property (nonatomic, strong) UIImageView *thirdImg;
@property (nonatomic, strong) UIImageView *fourthImg;

// 0 ~ 9
@property (nonatomic, strong) UIButton *zeroBtn;
@property (nonatomic, strong) UIButton *oneBtn;
@property (nonatomic, strong) UIButton *twoBtn;
@property (nonatomic, strong) UIButton *threeBtn;
@property (nonatomic, strong) UIButton *fourBtn;
@property (nonatomic, strong) UIButton *fiveBtn;
@property (nonatomic, strong) UIButton *sixBtn;
@property (nonatomic, strong) UIButton *sevenBtn;
@property (nonatomic, strong) UIButton *eightBtn;
@property (nonatomic, strong) UIButton *nineBtn;
@property (nonatomic, strong) UIButton *cancelBtn;

@property (nonatomic, assign) CGRect randomRact1;
@property (nonatomic, assign) CGRect randomRact2;
@property (nonatomic, assign) CGRect randomRact3;
@property (nonatomic, assign) CGRect randomRact4;
@property (nonatomic, assign) CGRect randomRact5;
@property (nonatomic, assign) CGRect randomRact6;
@property (nonatomic, assign) CGRect randomRact7;
@property (nonatomic, assign) CGRect randomRact8;
@property (nonatomic, assign) CGRect randomRact9;
@property (nonatomic, assign) CGRect randomRact0;

@property (nonatomic, strong) UIVisualEffectView *baseView;
@property (nonatomic, strong) NSString *keyword;
@property (nonatomic, assign) BOOL userEverLaunchedJudgement;
@property (nonatomic, strong) NSMutableArray *keyArr;
@property (nonatomic, strong) NSString *tempString;

@end

static NSInteger userLaunchedCount = 0; // 用于记录第一次登陆密码输入次数

@implementation PasswordVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    // 用户密码初始化
    if (self.setNewPassWord == YES) {
        [self userSetNewPassword];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIImageView *bgimg = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    bgimg.image = [UIImage imageNamed:@"keywordbase"];
    [self.view addSubview:bgimg];
    
    [self addSubViews];
    [UIView animateWithDuration:1.0 animations:^{
        self.baseView.alpha = 1.0;
    }];
    
    // 调用判断
    [self everLaunchedJudgment];
}

- (void)addSubViews
{
    if ([WYTDevicesTool iPhone4_iPhone4s]) {
        self.randomRact1 = CGRectMake(kNumberItemOrigineX, kNumberItemOrigineY_Iphone4, kNumberItemSide, kNumberItemSide);
        self.randomRact2 = CGRectMake(kNumberItemOrigineX + kNumberItemSide, kNumberItemOrigineY_Iphone4, kNumberItemSide, kNumberItemSide);
        self.randomRact3 = CGRectMake(kNumberItemOrigineX + kNumberItemSide*2, kNumberItemOrigineY_Iphone4, kNumberItemSide, kNumberItemSide);
        self.randomRact4 = CGRectMake(kNumberItemOrigineX, kNumberItemOrigineY_Iphone4 + kNumberItemSide, kNumberItemSide, kNumberItemSide);
        self.randomRact5 = CGRectMake(kNumberItemOrigineX + kNumberItemSide, kNumberItemOrigineY_Iphone4 + kNumberItemSide, kNumberItemSide, kNumberItemSide);
        self.randomRact6 = CGRectMake(kNumberItemOrigineX + kNumberItemSide*2, kNumberItemOrigineY_Iphone4 + kNumberItemSide, kNumberItemSide, kNumberItemSide);
        self.randomRact7 = CGRectMake(kNumberItemOrigineX, kNumberItemOrigineY_Iphone4 + kNumberItemSide*2, kNumberItemSide, kNumberItemSide);
        self.randomRact8 = CGRectMake(kNumberItemOrigineX + kNumberItemSide, kNumberItemOrigineY_Iphone4 + kNumberItemSide*2, kNumberItemSide, kNumberItemSide);
        self.randomRact9 = CGRectMake(kNumberItemOrigineX + kNumberItemSide*2, kNumberItemOrigineY_Iphone4 + kNumberItemSide*2, kNumberItemSide, kNumberItemSide);
        self.randomRact0 = CGRectMake(kNumberItemOrigineX + kNumberItemSide, kNumberItemOrigineY_Iphone4 + kNumberItemSide*3, kNumberItemSide, kNumberItemSide);
    } else {
        self.randomRact1 = CGRectMake(kNumberItemOrigineX, kNumberItemOrigineY, kNumberItemSide, kNumberItemSide);
        self.randomRact2 = CGRectMake(kNumberItemOrigineX + kNumberItemSide, kNumberItemOrigineY, kNumberItemSide, kNumberItemSide);
        self.randomRact3 = CGRectMake(kNumberItemOrigineX + kNumberItemSide*2, kNumberItemOrigineY, kNumberItemSide, kNumberItemSide);
        self.randomRact4 = CGRectMake(kNumberItemOrigineX, kNumberItemOrigineY + kNumberItemSide, kNumberItemSide, kNumberItemSide);
        self.randomRact5 = CGRectMake(kNumberItemOrigineX + kNumberItemSide, kNumberItemOrigineY + kNumberItemSide, kNumberItemSide, kNumberItemSide);
        self.randomRact6 = CGRectMake(kNumberItemOrigineX + kNumberItemSide*2, kNumberItemOrigineY + kNumberItemSide, kNumberItemSide, kNumberItemSide);
        self.randomRact7 = CGRectMake(kNumberItemOrigineX, kNumberItemOrigineY + kNumberItemSide*2, kNumberItemSide, kNumberItemSide);
        self.randomRact8 = CGRectMake(kNumberItemOrigineX + kNumberItemSide, kNumberItemOrigineY + kNumberItemSide*2, kNumberItemSide, kNumberItemSide);
        self.randomRact9 = CGRectMake(kNumberItemOrigineX + kNumberItemSide*2, kNumberItemOrigineY + kNumberItemSide*2, kNumberItemSide, kNumberItemSide);
        self.randomRact0 = CGRectMake(kNumberItemOrigineX + kNumberItemSide, kNumberItemOrigineY + kNumberItemSide*3, kNumberItemSide, kNumberItemSide);
    }
    
    NSValue *randomValue1 = [NSValue valueWithCGRect:self.randomRact1];
    NSValue *randomValue2 = [NSValue valueWithCGRect:self.randomRact2];
    NSValue *randomValue3 = [NSValue valueWithCGRect:self.randomRact3];
    NSValue *randomValue4 = [NSValue valueWithCGRect:self.randomRact4];
    NSValue *randomValue5 = [NSValue valueWithCGRect:self.randomRact5];
    NSValue *randomValue6 = [NSValue valueWithCGRect:self.randomRact6];
    NSValue *randomValue7 = [NSValue valueWithCGRect:self.randomRact7];
    NSValue *randomValue8 = [NSValue valueWithCGRect:self.randomRact8];
    NSValue *randomValue9 = [NSValue valueWithCGRect:self.randomRact9];
    NSValue *randomValue0 = [NSValue valueWithCGRect:self.randomRact0];
    
    NSMutableArray *randomArray = [NSMutableArray array];
    [randomArray addObject:randomValue1];
    [randomArray addObject:randomValue2];
    [randomArray addObject:randomValue3];
    [randomArray addObject:randomValue4];
    [randomArray addObject:randomValue5];
    [randomArray addObject:randomValue6];
    [randomArray addObject:randomValue7];
    [randomArray addObject:randomValue8];
    [randomArray addObject:randomValue9];
    [randomArray addObject:randomValue0];
    id temp;
    for (int i = 0; i < randomArray.count; i++) {
        int index = arc4random() % (randomArray.count - i) + i;
        if (index != i) {
            temp = randomArray[i];
            randomArray[i] = randomArray[index];
            randomArray[index] = temp;
        }
    }
    
    self.baseView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
    self.baseView.frame = self.view.frame;
    self.baseView.alpha = 0.6;
    [self.view addSubview:self.baseView];
    
    if ([WYTDevicesTool iPhone4_iPhone4s]) {
        self.topView =  [[UIView alloc] initWithFrame:CGRectMake(kTopViewOrigineX, kTopViewOrigineY_Iphone4, kNumberItemSide*3*0.75, kTopViewSizeH)];
    } else {
        self.topView =  [[UIView alloc] initWithFrame:CGRectMake(kTopViewOrigineX, kTopViewOrigineY, kNumberItemSide*3*0.75, kTopViewSizeH)];
    }
    [self.baseView addSubview:self.topView];
    
    self.firstImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kTopImageSizeW, kTopViewSizeH)];
    self.firstImg.image = [UIImage imageNamed:@"topImg_normal"];
    [self.topView addSubview:self.firstImg];
    
    self.secondImg = [[UIImageView alloc] initWithFrame:CGRectMake(kTopImageSizeW, 0, kTopImageSizeW, kTopViewSizeH)];
    self.secondImg.image = [UIImage imageNamed:@"topImg_normal"];
    [self.topView addSubview:self.secondImg];
    
    self.thirdImg = [[UIImageView alloc] initWithFrame:CGRectMake(kTopImageSizeW*2, 0, kTopImageSizeW, kTopViewSizeH)];
    self.thirdImg.image = [UIImage imageNamed:@"topImg_normal"];
    [self.topView addSubview:self.thirdImg];
    
    self.fourthImg = [[UIImageView alloc] initWithFrame:CGRectMake(kTopImageSizeW*3, 0, kTopImageSizeW, kTopViewSizeH)];
    self.fourthImg.image = [UIImage imageNamed:@"topImg_normal"];
    [self.topView addSubview:self.fourthImg];
    
    // 前方高能 → → Buttons
    self.oneBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.oneBtn.frame = [randomArray[0] CGRectValue];;
    [self.oneBtn setBackgroundImage:[UIImage imageNamed:@"number_01"] forState:0];
    [self.oneBtn addTarget:self action:@selector(chickBtnDown:) forControlEvents:1<<0];
    [self.oneBtn addTarget:self action:@selector(chickNumberBtn:) forControlEvents:1<<6];
    self.oneBtn.tag = kOneBtnTag;
    [self.baseView addSubview:self.oneBtn];
    
    self.twoBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.twoBtn.frame = [randomArray[1] CGRectValue];
    [self.twoBtn setBackgroundImage:[UIImage imageNamed:@"number_02"] forState:0];
    [self.twoBtn addTarget:self action:@selector(chickBtnDown:) forControlEvents:1<<0];
    [self.twoBtn addTarget:self action:@selector(chickNumberBtn:) forControlEvents:1<<6];
    self.twoBtn.tag = kTwoBtnTag;
    [self.baseView addSubview:self.twoBtn];
    
    self.threeBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.threeBtn.frame = [randomArray[2] CGRectValue];;
    [self.threeBtn setBackgroundImage:[UIImage imageNamed:@"number_03"] forState:0];
    [self.threeBtn addTarget:self action:@selector(chickBtnDown:) forControlEvents:1<<0];
    [self.threeBtn addTarget:self action:@selector(chickNumberBtn:) forControlEvents:1<<6];
    self.threeBtn.tag = kThreeBtnTag;
    [self.baseView addSubview:self.threeBtn];
    
    self.fourBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.fourBtn.frame = [randomArray[3] CGRectValue];;
    [self.fourBtn setBackgroundImage:[UIImage imageNamed:@"number_04"] forState:0];
    [self.fourBtn addTarget:self action:@selector(chickBtnDown:) forControlEvents:1<<0];
    [self.fourBtn addTarget:self action:@selector(chickNumberBtn:) forControlEvents:1<<6];
    self.fourBtn.tag = kFourBtnTag;
    [self.baseView addSubview:self.fourBtn];
    
    self.fiveBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.fiveBtn.frame = [randomArray[4] CGRectValue];;
    [self.fiveBtn setBackgroundImage:[UIImage imageNamed:@"number_05"] forState:0];
    [self.fiveBtn addTarget:self action:@selector(chickBtnDown:) forControlEvents:1<<0];
    [self.fiveBtn addTarget:self action:@selector(chickNumberBtn:) forControlEvents:1<<6];
    self.fiveBtn.tag = kFiveBtnTag;
    [self.baseView addSubview:self.fiveBtn];
    
    self.sixBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.sixBtn.frame = [randomArray[5] CGRectValue];;
    [self.sixBtn setBackgroundImage:[UIImage imageNamed:@"number_06"] forState:0];
    [self.sixBtn addTarget:self action:@selector(chickBtnDown:) forControlEvents:1<<0];
    [self.sixBtn addTarget:self action:@selector(chickNumberBtn:) forControlEvents:1<<6];
    self.sixBtn.tag = kSixBtnTag;
    [self.baseView addSubview:self.sixBtn];
    
    self.sevenBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.sevenBtn.frame = [randomArray[6] CGRectValue];;
    [self.sevenBtn setBackgroundImage:[UIImage imageNamed:@"number_07"] forState:0];
    [self.sevenBtn addTarget:self action:@selector(chickBtnDown:) forControlEvents:1<<0];
    [self.sevenBtn addTarget:self action:@selector(chickNumberBtn:) forControlEvents:1<<6];
    self.sevenBtn.tag = kSevenBtnTag;
    [self.baseView addSubview:self.sevenBtn];
    
    self.eightBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.eightBtn.frame = [randomArray[7] CGRectValue];;
    [self.eightBtn setBackgroundImage:[UIImage imageNamed:@"number_08"] forState:0];
    [self.eightBtn addTarget:self action:@selector(chickBtnDown:) forControlEvents:1<<0];
    [self.eightBtn addTarget:self action:@selector(chickNumberBtn:) forControlEvents:1<<6];
    self.eightBtn.tag = kEightBtnTag;
    [self.baseView addSubview:self.eightBtn];
    
    self.nineBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.nineBtn.frame = [randomArray[8] CGRectValue];;
    [self.nineBtn setBackgroundImage:[UIImage imageNamed:@"number_09"] forState:0];
    [self.nineBtn addTarget:self action:@selector(chickBtnDown:) forControlEvents:1<<0];
    [self.nineBtn addTarget:self action:@selector(chickNumberBtn:) forControlEvents:1<<6];
    self.nineBtn.tag = kNineBtnTag;
    [self.baseView addSubview:self.nineBtn];
    
    self.zeroBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.zeroBtn.frame = [randomArray[9] CGRectValue];;
    [self.zeroBtn setBackgroundImage:[UIImage imageNamed:@"number_00"] forState:0];
    [self.zeroBtn addTarget:self action:@selector(chickBtnDown:) forControlEvents:1<<0];
    [self.zeroBtn addTarget:self action:@selector(chickNumberBtn:) forControlEvents:1<<6];
    self.zeroBtn.tag = kZeroBtnTag;
    [self.baseView addSubview:self.zeroBtn];
    
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    if ([WYTDevicesTool iPhone4_iPhone4s]) {
        self.cancelBtn.frame = CGRectMake(kNumberItemOrigineX + kNumberItemSide, kNumberItemOrigineY_Iphone4 + kNumberItemSide*4, kNumberItemSide, kNumberItemSide);
    } else {
        self.cancelBtn.frame = CGRectMake(kNumberItemOrigineX + kNumberItemSide, kNumberItemOrigineY + kNumberItemSide*4, kNumberItemSide, kNumberItemSide);
    }
    [self.cancelBtn setBackgroundImage:[UIImage imageNamed:@"number_x"] forState:0];
    [self.cancelBtn addTarget:self action:@selector(chickCancelBtn:) forControlEvents:1<<0];
    self.cancelBtn.tag = kCancelBtnTag;
    [self.baseView addSubview:self.cancelBtn];
}

- (void)chickBtnDown:(UIButton *)sender
{
    NSString *tempStr = [NSString stringWithFormat:@"%ld", sender.tag - 286010];
    [self.keyArr addObject:tempStr];
    self.keyword = [self.keyArr componentsJoinedByString:@""];
    switch (self.keyArr.count) {
        case 1:
            self.firstImg.image = [UIImage imageNamed:@"topImg_selected"];
            break;
        case 2:
            self.secondImg.image = [UIImage imageNamed:@"topImg_selected"];
            break;
        case 3:
            self.thirdImg.image = [UIImage imageNamed:@"topImg_selected"];
            break;
        case 4:
            self.fourthImg.image = [UIImage imageNamed:@"topImg_selected"];
            break;
        default:
            break;
    }
}

- (void)chickNumberBtn:(UIButton *)sender
{
    if (self.keyArr.count == 4) {
        [self judgmentAndReset];
    }
}

- (void)judgmentAndReset
{
    self.firstImg.image = [UIImage imageNamed:@"topImg_normal"];
    self.secondImg.image = [UIImage imageNamed:@"topImg_normal"];
    self.thirdImg.image = [UIImage imageNamed:@"topImg_normal"];
    self.fourthImg.image = [UIImage imageNamed:@"topImg_normal"];
    WYTDeskVC *deskVC = [[WYTDeskVC alloc] init];
    if (self.userEverLaunchedJudgement == YES) {
        if (userLaunchedCount == 0) {
            self.tempString = self.keyword;
            UIAlertController *repateAlert = [UIAlertController alertControllerWithTitle:@"重复上次密码" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sureAc = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                userLaunchedCount  = 1;
                [self.keyArr removeAllObjects];
            }];
            [repateAlert addAction:sureAc];
            [self presentViewController:repateAlert animated:YES completion:nil];
        } else if (userLaunchedCount == 1){
            if ([self.keyword isEqualToString:self.tempString]) {
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                [userDefaults setObject:self.keyword forKey:@"userKeyWords"];
                //赋值换到添加教程页面
                //[userDefaults setBool:NO forKey:@"firstLaunch"];
                [userDefaults synchronize];
                [self presentViewController:deskVC animated:YES completion:nil];
                [self.keyArr removeAllObjects];
            }else {
                UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:@"输入错误请重新输入" message:nil preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *sureAc = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    userLaunchedCount  = 0;
                    [self.keyArr removeAllObjects];
                }];
                [errorAlert addAction:sureAc];
                [self presentViewController:errorAlert animated:YES completion:nil];
            }
        }
    } else {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        NSString *userKeyWords = [userDefaults objectForKey:@"userKeyWords"];
        if (userKeyWords.length == 0) {
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:self.keyword forKey:@"userKeyWords"];
            [userDefaults synchronize];
//            [self presentViewController:deskVC animated:YES completion:nil];
            UIAlertController *repateAlert = [UIAlertController alertControllerWithTitle:@"重复上次密码" message:nil preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sureAc = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                [self.keyArr removeAllObjects];
            }];
            [repateAlert addAction:sureAc];
            [self presentViewController:repateAlert animated:YES completion:nil];
        } else {
            if ([self.keyword isEqualToString:userKeyWords]) {
                [self presentViewController:deskVC animated:YES completion:nil];
                [self.keyArr removeAllObjects];
            } else {
                UIAlertController *errorAlert = [UIAlertController alertControllerWithTitle:@"密码错误" message:nil preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *sureAc = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self.keyArr removeAllObjects];
                }];
                [errorAlert addAction:sureAc];
                [self presentViewController:errorAlert animated:YES completion:nil];
            }
        }
    }
}

- (void)chickCancelBtn:(UIButton *)sender
{
    if (self.keyArr.count > 0) {
        [self.keyArr removeLastObject];
    }
    switch (self.keyArr.count) {
        case 0:
            self.firstImg.image = [UIImage imageNamed:@"topImg_normal"];
            self.secondImg.image = [UIImage imageNamed:@"topImg_normal"];
            self.thirdImg.image = [UIImage imageNamed:@"topImg_normal"];
            self.fourthImg.image = [UIImage imageNamed:@"topImg_normal"];
            break;
        case 1:
            self.firstImg.image = [UIImage imageNamed:@"topImg_selected"];
            self.secondImg.image = [UIImage imageNamed:@"topImg_normal"];
            self.thirdImg.image = [UIImage imageNamed:@"topImg_normal"];
            self.fourthImg.image = [UIImage imageNamed:@"topImg_normal"];
            break;
        case 2:
            self.secondImg.image = [UIImage imageNamed:@"topImg_selected"];
            self.thirdImg.image = [UIImage imageNamed:@"topImg_normal"];
            self.fourthImg.image = [UIImage imageNamed:@"topImg_normal"];
            break;
        case 3:
            self.thirdImg.image = [UIImage imageNamed:@"topImg_selected"];
            self.fourthImg.image = [UIImage imageNamed:@"topImg_normal"];
            break;
        default:
            break;
    }
}

#pragma mark 用户密码更新
- (void)userSetNewPassword
{
    UIAlertController *setNewPwAc = [UIAlertController alertControllerWithTitle:@"请输入您的新密码" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAc = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [userDefaults setObject:nil forKey:@"userKeyWords"];
        [userDefaults synchronize];
        self.setNewPassWord = NO;
    }];
    [setNewPwAc addAction:sureAc];
    [self presentViewController:setNewPwAc animated:YES completion:nil];
}

- (NSMutableArray *)keyArr
{
    if (!_keyArr) {
        _keyArr = [NSMutableArray array];
    }
    return _keyArr;
}

#pragma mark PasswordJudgment
- (void)everLaunchedJudgment
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        // 这里判断是否是第一次
        self.userEverLaunchedJudgement = YES;
    } else {
        self.userEverLaunchedJudgement = NO;
    }
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    if (self.userEverLaunchedJudgement == YES) {
        UIAlertController *enterPasswordPrompt = [UIAlertController alertControllerWithTitle:@"为了您的隐私安全" message:@"请设置4位个人密码" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"设置密码" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [enterPasswordPrompt addAction:sureAction];
        [self presentViewController:enterPasswordPrompt animated:YES completion:nil];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
